<?php

namespace App\Http\Controllers\Admin;

use App\News;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;

class NewsController extends Controller
{
    /*
     |--------------------------------------------------------------------------
     |  新闻类型定义:
     |  1. 小儿推拿家长精品班
     |  2. 母乳喂养课堂
     |  3. 家庭早教指导课程
     |  4. 生养教前沿咨询
     |--------------------------------------------------------------------------
     */

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        $data = array();
        $pageSize = 10;
        $news = News::where('isDeleted',0)->paginate($pageSize);

        $data['newsList'] = $news;
        return view("admin.news.listnews",$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate(Request $request)
    {
        $data = array();
        $id = $request->get('id');
        if ($id){
            $news = News::where('id',$id)->first();
            if ($news) {
                $data['news'] = $news;
            }
        }
        $types = array(
            Config::get('constants.PARENT_SCHOOL_TYPE1')['key'] => Config::get('constants.PARENT_SCHOOL_TYPE1')['val'],
            Config::get('constants.PARENT_SCHOOL_TYPE2')['key'] => Config::get('constants.PARENT_SCHOOL_TYPE2')['val'],
            Config::get('constants.PARENT_SCHOOL_TYPE3')['key'] => Config::get('constants.PARENT_SCHOOL_TYPE3')['val'],
            Config::get('constants.PARENT_SCHOOL_TYPE4')['key'] => Config::get('constants.PARENT_SCHOOL_TYPE4')['val']
        );
        $data['types'] = $types;

        return view('admin.news.addnews',$data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postSave(Request $request)
    {
        $id = $request->id;

        $now = time();
        if ($id) { // 更新
            $news = News::where('id',$id)->first();
        } else { // 插入
            $news = new News();
            $news->created = $now;
        }

        $news->type = $request->get("type");
        $news->title = $request->get("title");
        $news->content = $request->get("content");
        $news->updated = $now;
        if ($news->save()){
            return redirect("admin/news/index");
        } else {
            return redirect()->back()->withInput()->withErrors('保存失败！');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postDelete(Request $request)
    {
        $data = array();
        $id = $request->get('id');
        if (!$id) {
            $data['code'] = 1002;
            $data['msg']  = '操作失败';
            echo json_encode($data);
            exit;
        }

        $update = array(
            'isDeleted' => 1
        );
        $res = News::where('id',$id)->where('isDeleted',0)->update($update);

        if ($res) {
            $data['code'] = 1001;
            $data['msg']  = '操作成功';
            echo json_encode($data);
            exit;
        } else {
            $data['code'] = 1002;
            $data['msg']  = '操作失败';
            echo json_encode($data);
            exit;
        }
    }
}
